package 笔试.小米;

import java.util.ArrayList;
import java.util.Scanner;

public class One {
    static int T;
    static int[][] grid = new int[3][3];
    static boolean[] used = new boolean[10];
    static int count;
    static ArrayList<int[]> emptyCells;
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        T = scanner.nextInt();
        for (int t = 0; t < T; t++) {
            grid = new int[3][3];
            used = new boolean[10]; // Reset used numbers
            emptyCells = new ArrayList<>();
            count = 0;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    grid[i][j] = scanner.nextInt();
                    int num = grid[i][j];
                    if (num == 0) {
                        emptyCells.add(new int[]{i, j});
                    } else {
                        if (used[num]) {
                            System.out.println(0);
                            continue;
                        }
                        used[num] = true;
                    }
                }
            }
            ArrayList<Integer> remainingNumbers = new ArrayList<>();
            for (int num = 1; num <= 9; num++) {
                if (!used[num]) {
                    remainingNumbers.add(num);
                }
            }
            dfs(0, remainingNumbers);
            System.out.println(count);
        }
        scanner.close();
    }

    static void dfs(int index, ArrayList<Integer> remainingNumbers) {
        if (index == emptyCells.size()) {
            if (checkAdjacency()) {
                count++;
            }
            return;
        }
        int[] cell = emptyCells.get(index);
        int i = cell[0];
        int j = cell[1];
        for (int k = 0; k < remainingNumbers.size(); k++) {
            int num = remainingNumbers.get(k);
            grid[i][j] = num;
            used[num] = true;
            remainingNumbers.remove(k);
            if (isValidPlacement(i, j)) {
                dfs(index + 1, remainingNumbers);
            }
            grid[i][j] = 0;
            used[num] = false;
            remainingNumbers.add(k, num);
        }
    }

    static boolean isValidPlacement(int i, int j) {
        int num = grid[i][j];
        for (int d = 0; d < 4; d++) {
            int ni = i + dx[d];
            int nj = j + dy[d];
            if (ni >= 0 && ni < 3 && nj >= 0 && nj < 3) {
                int adjNum = grid[ni][nj];
                if (adjNum != 0 && Math.abs(num - adjNum) == 1) {
                    return false;
                }
            }
        }
        return true;
    }

    static boolean checkAdjacency() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                int num = grid[i][j];
                for (int d = 0; d < 4; d++) {
                    int ni = i + dx[d];
                    int nj = j + dy[d];
                    if (ni >= 0 && ni < 3 && nj >= 0 && nj < 3) {
                        int adjNum = grid[ni][nj];
                        if (adjNum != 0 && Math.abs(num - adjNum) == 1) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
}